﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MyCleverMissionEditor
{
    public class Slot : PictureBox
    {
        public enum DirectionType
        {
            Input,
            Output
        }

        private DirectionType slotType;
        private Rectangle arrowRectangle;
        private bool isActive;

        public DirectionType SlotType
        {
            get
            {
                return this.slotType;
            }
        }

        public Rectangle ArrowRectangle
        {
            get
            {
                return this.arrowRectangle;
            }
            set
            {
                this.arrowRectangle = value;
                this.Location = this.GetLocation();
            }
        }

        public Point[] Lines
        {
            get
            {
                Point[] lines = new Point[3];
                if (DirectionType.Output == slotType)
                {
                    lines[0] = new Point(this.arrowRectangle.Left, this.arrowRectangle.Top);
                    lines[1] = new Point(this.arrowRectangle.Left, this.arrowRectangle.Bottom);
                    lines[2] = new Point(this.arrowRectangle.Right, this.arrowRectangle.Bottom);
                }
                else
                {
                    lines[0] = new Point(this.arrowRectangle.Right, this.arrowRectangle.Bottom);
                    lines[1] = new Point(this.arrowRectangle.Right, this.arrowRectangle.Top);
                    lines[2] = new Point(this.arrowRectangle.Left, this.arrowRectangle.Top);
                }
                return lines;
            }
        }

        public bool IsActive
        {
            get
            {
                return this.isActive;
            }
        }

        public Slot(DirectionType slotType, Rectangle arrowRectangle, bool isActive)
        {
            this.slotType = slotType;
            this.arrowRectangle = arrowRectangle;
            this.isActive = isActive;

            this.UpdateImage(false);
            this.BackColor = Color.Transparent;
            this.Size = new Size(12, 12);
            this.Location = GetLocation();      
            
            this.MouseEnter += new System.EventHandler(OnMouseEnter);
            this.MouseLeave += new System.EventHandler(OnMouseLeave);
            this.MouseClick += new MouseEventHandler(OnClick);
        }

        private void OnMouseEnter(object sender, System.EventArgs e)
        {
            this.UpdateImage(true);

            if (DirectionType.Input == slotType)
            {
                this.Location = new Point(this.Location.X - 4, this.Location.Y - 9);
            }
            else
            {
                this.Location = new Point(this.Location.X - 4, this.Location.Y);
            }     
            
            this.Size = new Size(24, 24);
        }

        private void OnMouseLeave(object sender, System.EventArgs e)
        {
            this.UpdateImage(false);

            if (DirectionType.Input == slotType)
            {
                this.Location = new Point(this.Location.X + 4, this.Location.Y + 9);
            }
            else
            {
                this.Location = new Point(this.Location.X + 4, this.Location.Y);
            }
   
            this.Size = new Size(12, 12);
        }

        private void OnClick(object sender, MouseEventArgs e)
        {
            this.isActive = !this.isActive;
            this.Parent.Parent.Parent.Refresh();
        }

        private void UpdateImage(bool useBigPicture)
        {
            if (DirectionType.Input == this.slotType)
            {
                if (false == useBigPicture)
                {
                    this.Image = global::MyCleverMissionEditor.Properties.Resources.collector;
                }
                else
                {
                    this.Image = global::MyCleverMissionEditor.Properties.Resources.collector_big_green;
                }
            }
            else
            {
                if (false == useBigPicture)
                {
                    this.Image = global::MyCleverMissionEditor.Properties.Resources.slot;
                }
                else
                {
                    this.Image = global::MyCleverMissionEditor.Properties.Resources.slot_big_green;
                }
            }
        }

        private Point GetLocation()
        {        
            //                                  ----------------------
            //                                  |                    |
            //                                  |                    |
            //                                  --------------*Slot*--
            if (DirectionType.Input == slotType)
            {
                return new Point(this.arrowRectangle.Right - 5 , this.arrowRectangle.Bottom - this.Height);
            }
            //                                              --*Slot*--------------
            //                                              |                    |
            //                                              |                    |
            //                                              ----------------------
            else
            {
                return new Point(this.arrowRectangle.Location.X - 5, this.arrowRectangle.Location.Y);
            }
        }
    }
}
